package info.motteke.kurumi.swt;


import info.motteke.kurumi.VoiceDetail;
import info.motteke.mixivoice4j.Entry;
import info.motteke.mixivoice4j.User;
import info.motteke.mixivoice4j.Voice;
import info.motteke.swt.LayoutText;

import java.text.SimpleDateFormat;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;

class SwtVoiceDetail implements VoiceDetail {

    private final SwtImageCache imageCache;

    private final Composite base;

    private Voice displayingVoice;

    private final Composite textField;

    private final Composite labelField;

    private final Composite imageField;

    private final Label userThumbnail;

    private final Link imageLink;

    private final Label userNickname;

    private final Label postDate;

    private final LayoutText body;

    private final SwtVoiceFavoriteList favorites;

    public SwtVoiceDetail(Composite parent, SwtImageCache imageCache) {
        this.imageCache = imageCache;
        base = createBase(parent);
        imageField = new Composite(base, SWT.NONE);
        GridLayout layout = new GridLayout(1, true);
        layout.horizontalSpacing = 0;
        layout.verticalSpacing = 0;
        imageField.setLayout(layout);
        GridData gd;

        gd = new GridData();
        gd.horizontalIndent = 0;
        gd.verticalIndent = 0;
        imageField.setLayoutData(gd);
        userThumbnail = createUserThumbnail(imageField);
        imageLink = new Link(imageField, SWT.NONE);
        imageLink.setText("画像");
        imageLink.addSelectionListener(new ImageSelectionListener(base, imageCache));
        textField = createTextField(base);
        labelField = createLabelField(textField);
        userNickname = new Label(labelField, SWT.LEFT);
        postDate = new Label(labelField, SWT.RIGHT);
        body = createBody(textField, textField);

        favorites = new SwtVoiceFavoriteList(textField);
    }

    private Composite createBase(Composite parent){
        Composite c = new Composite(parent, SWT.BORDER);
        GridLayout layout = new GridLayout(2, false);
        layout.marginHeight = 1;
        layout.marginWidth = 1;
        c.setLayout(layout);
        return c;
    }

    private Label createUserThumbnail(Composite parent) {
        Label l = new Label(parent, SWT.BORDER);
        l.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
        ClassLoader cl = getClass().getClassLoader();
        l.setImage(new Image(parent.getDisplay(), cl.getResourceAsStream("dummy.png")));
        return l;
    }

    private Composite createTextField(Composite parent) {
        Composite c = new Composite(parent, SWT.NULL);
        GridLayout layout = new GridLayout(1, true);
        layout.marginHeight = 0;
        layout.marginWidth = 0;

        c.setLayout(layout);
        GridData gd = new GridData(GridData.FILL_BOTH);
        gd.horizontalIndent = 0;
        gd.verticalIndent = 0;
        c.setLayoutData(gd);
        return c;
    }

    private Composite createLabelField(Composite parent) {
        Composite c = new Composite(parent, SWT.NULL);
        c.setBackground(ColorPattern.WHITE);
        c.setLayout(new FillLayout());
        GridData gd = new GridData(GridData.FILL_HORIZONTAL);
        gd.horizontalIndent = 2;
        gd.verticalIndent = 2;
        c.setLayoutData(gd);
        return c;
    }

    public LayoutText createBody(Composite parent, Control topBase) {
        LayoutText t = new LayoutText(parent, SWT.NONE);
        GridData gd = new GridData(GridData.FILL_BOTH);
        gd.horizontalIndent = 0;
        gd.verticalIndent = 0;
        t.setLayoutData(gd);
        return t;
    }

    /* (非 Javadoc)
     * @see info.motteke.kurumi.swt.VoiceDetail#showVoice(mixivoice4j.Voice)
     */
    public void showVoice(Voice voice) {
        base.getDisplay().syncExec(new VoiceUpdate(this, voice));
    }

    /* package */ void showVoiceImpl(Voice voice) {
        this.displayingVoice = voice;
        base.setData(voice);
        User user = voice.getUser();
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");

        userThumbnail.setImage(imageCache.getImage(user.getThumbnail().getUrl(), 48, 48));

        userNickname.setText(user.getNickName());
        postDate.setText(format.format(voice.getPostDate()));
        body.setText(voice.getBody().toString());
        body.redraw();

        if(voice instanceof Entry && ((Entry)voice).getImageThumbnailUrl() != null){
            imageLink.setText("<a>画像</a>");
        } else {
            imageLink.setText("画像");
        }

        favorites.addFavorite(voice.getParent().getAllFavorites());
    }

    @Override
    public Voice getDisplayingVoice() {
        return displayingVoice;
    }
}
